//
//  UIViewController+YYTransitioning.m
//  YYTransitionAnimation
//
//  Created by 袁培超 on 2024/3/26.
//

#import "UIViewController+YYTransitioning.h"
#import "YYPushTransitioningDelegate.h"
#import "YYPresentTransitioningDelegate.h"
#import <objc/runtime.h>

@interface UIViewController ()

@property (nonatomic, strong) YYPushTransitioningDelegate *pushTransitioningDelegate;
@property (nonatomic, strong) YYPresentTransitioningDelegate *presentTransitioningDelegate;

@end

@implementation UIViewController (YYTransitioning)

- (void)setPushTransitioningDelegate:(YYPushTransitioningDelegate *)pushTransitioningDelegate
{
    objc_setAssociatedObject(self, @selector(pushTransitioningDelegate), pushTransitioningDelegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (YYPushTransitioningDelegate *)pushTransitioningDelegate
{
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setPresentTransitioningDelegate:(YYPresentTransitioningDelegate *)presentTransitioningDelegate
{
    objc_setAssociatedObject(self, @selector(presentTransitioningDelegate), presentTransitioningDelegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (YYPresentTransitioningDelegate *)presentTransitioningDelegate
{
    return objc_getAssociatedObject(self, _cmd);
}


- (void)yy_showViewController:(UIViewController *)viewControllerToShow
                   transitioning:(kTransitioningType)transitioningType
                     interacting:(BOOL)interacting
                      completion:(void (^ __nullable)(void))completion
{
    if (transitioningType == kTransitioningTypePush) {
        self.pushTransitioningDelegate = [[YYPushTransitioningDelegate alloc] init];
        [self.pushTransitioningDelegate configureTransition:viewControllerToShow interacting:interacting];
        
        viewControllerToShow.transitioningDelegate = self.pushTransitioningDelegate;
        viewControllerToShow.modalPresentationStyle = UIModalPresentationCustom;
        [self presentViewController:viewControllerToShow animated:YES completion:completion];
    } else {
        self.presentTransitioningDelegate = [[YYPresentTransitioningDelegate alloc] init];
        [self.presentTransitioningDelegate configureTransition:viewControllerToShow interacting:interacting];
        
        viewControllerToShow.transitioningDelegate = self.presentTransitioningDelegate;
        viewControllerToShow.modalPresentationStyle = UIModalPresentationCustom;
        [self presentViewController:viewControllerToShow animated:YES completion:completion];
    }
}

@end
